﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.StorageForecast;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.DBUtility;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.SqlServerDAL.StorageForecast
{
    /// <summary>
    /// 数据访问类:QuotaPrompt
    /// </summary>
    public partial class D_QuotaPrompt : I_QuotaPrompt
    {
        public D_QuotaPrompt()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int PromptID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from QuotaPrompt");
            strSql.Append(" where PromptID=@PromptID");
            SqlParameter[] parameters = {
					new SqlParameter("@PromptID", SqlDbType.Int,4)
};
            parameters[0].Value = PromptID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_QuotaPrompt model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into QuotaPrompt(");
            strSql.Append("QuotaID,RawTypeID,StartNum,EndNum,Prompt)");
            strSql.Append(" values (");
            strSql.Append("@QuotaID,@RawTypeID,@StartNum,@EndNum,@Prompt)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@RawTypeID", SqlDbType.Int,4),
					new SqlParameter("@StartNum", SqlDbType.Float,8),
					new SqlParameter("@EndNum", SqlDbType.Float,8),
					new SqlParameter("@Prompt", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.QuotaID;
            parameters[1].Value = model.RawTypeID;
            parameters[2].Value = model.StartNum;
            parameters[3].Value = model.EndNum;
            parameters[4].Value = model.Prompt;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_QuotaPrompt model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update QuotaPrompt set ");
            strSql.Append("QuotaID=@QuotaID,");
            strSql.Append("RawTypeID=@RawTypeID,");
            strSql.Append("StartNum=@StartNum,");
            strSql.Append("EndNum=@EndNum,");
            strSql.Append("Prompt=@Prompt");
            strSql.Append(" where PromptID=@PromptID");
            SqlParameter[] parameters = {
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@RawTypeID", SqlDbType.Int,4),
					new SqlParameter("@StartNum", SqlDbType.Float,8),
					new SqlParameter("@EndNum", SqlDbType.Float,8),
					new SqlParameter("@Prompt", SqlDbType.NVarChar,200),
					new SqlParameter("@PromptID", SqlDbType.Int,4)};
            parameters[0].Value = model.QuotaID;
            parameters[1].Value = model.RawTypeID;
            parameters[2].Value = model.StartNum;
            parameters[3].Value = model.EndNum;
            parameters[4].Value = model.Prompt;
            parameters[5].Value = model.PromptID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PromptID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QuotaPrompt ");
            strSql.Append(" where PromptID=@PromptID");
            SqlParameter[] parameters = {
					new SqlParameter("@PromptID", SqlDbType.Int,4)
};
            parameters[0].Value = PromptID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string PromptIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QuotaPrompt ");
            strSql.Append(" where PromptID in (" + PromptIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_QuotaPrompt GetModel(int PromptID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PromptID,QuotaID,RawTypeID,StartNum,EndNum,Prompt from QuotaPrompt ");
            strSql.Append(" where PromptID=@PromptID");
            SqlParameter[] parameters = {
					new SqlParameter("@PromptID", SqlDbType.Int,4)
};
            parameters[0].Value = PromptID;

            E_QuotaPrompt model = new E_QuotaPrompt();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PromptID"].ToString() != "")
                {
                    model.PromptID = int.Parse(ds.Tables[0].Rows[0]["PromptID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["QuotaID"].ToString() != "")
                {
                    model.QuotaID = int.Parse(ds.Tables[0].Rows[0]["QuotaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawTypeID"].ToString() != "")
                {
                    model.RawTypeID = int.Parse(ds.Tables[0].Rows[0]["RawTypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartNum"].ToString() != "")
                {
                    model.StartNum = decimal.Parse(ds.Tables[0].Rows[0]["StartNum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndNum"].ToString() != "")
                {
                    model.EndNum = decimal.Parse(ds.Tables[0].Rows[0]["EndNum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Prompt"] != null)
                {
                    model.Prompt = ds.Tables[0].Rows[0]["Prompt"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PromptID,QuotaID,RawTypeID,StartNum,EndNum,Prompt ");
            strSql.Append(" FROM QuotaPrompt ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PromptID,QuotaID,RawTypeID,StartNum,EndNum,Prompt ");
            strSql.Append(" FROM QuotaPrompt ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "QuotaPrompt";
            parameters[1].Value = "PromptID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
